package team.archai.nexus.boot.platform.core.granter;


import org.apache.commons.collections.CollectionUtils;
import team.archai.nexus.boot.auth.constant.AuthConstant;
import team.archai.nexus.boot.auth.exception.UserNotFountException;
import team.archai.nexus.boot.auth.model.AuthenticatedInfo;
import team.archai.nexus.boot.auth.util.AuthUtil;
import team.archai.nexus.boot.tool.Tool;

import java.util.List;

/**
 * @program: ITokenGranter
 * @description: 授权接口
 * @author: lishibin
 * @create: 20:37
 **/
public interface ITokenGranter {

    /**
     * @program: ITokenGranter
     * @description: 通过auth token 信息获取账户信息
     * @author: lishibin
     * @create: 20:41
     **/
    AuthenticatedInfo grant(AuthParameter authParameter);


    /**
     * 解密authKey
     *
     * @param authKey
     * @param signDate
     * @return
     */
    default List<String> decryptAuthKey(String authKey, String signDate) {
        String usernamePassword = AuthUtil.decryptEcb(authKey, signDate);
        if (Tool.isEmpty(usernamePassword)) {
            throw new UserNotFountException("Parse Auth Info failed.");
        }
        List<String> combineAccount = Tool.toStrList(AuthConstant.UserPassword.ENCRYPT_STRING, usernamePassword);
        if (CollectionUtils.isEmpty(combineAccount) || combineAccount.size() < 2) {
            throw new UserNotFountException("Parse Auth Info failed.");
        }
        return combineAccount;
    }
}
